"""
指数成分股
"""
'''
Created on 2020年1月30日
获取所有股票数据
_index_weight_中证全指_红利低波.py
@author: JM
'''
import pandas as pd

from app_config import get_pro, get_engine_ts


def get_data_range(start_date, end_date):
    df_zzqz = get_pro().index_weight(index_code='930955.CSI', start_date=start_date, end_date=end_date)
    df_zzqz.rename(columns={'con_code': 'ts_code'}, inplace=True)
    df_zzqz['symbol'] = df_zzqz['ts_code'].str.split('.').str[0]
    df_zzqz.drop(columns=['weight'], inplace=True)
    df_zzqz.to_excel('zfile/红利低波及中证全指/' + start_date + '+_930955.xlsx')

    df_zzqz = get_pro().index_weight(index_code='000985.CSI', start_date=start_date, end_date=end_date)
    df_zzqz.rename(columns={'con_code': 'ts_code'}, inplace=True)
    df_zzqz['symbol'] = df_zzqz['ts_code'].str.split('.').str[0]
    df_zzqz.drop(columns=['weight'], inplace=True)
    df_zzqz.to_excel('zfile/红利低波及中证全指/' + start_date + '_000985.xlsx')


def get_data(start_date):
    df_930955 = get_pro().index_weight(index_code='930955.CSI', start_date=start_date, end_date=start_date)
    df_930955.rename(columns={'con_code': 'ts_code'}, inplace=True)
    df_930955['symbol'] = df_930955['ts_code'].str.split('.').str[0]
    df_930955.drop(columns=['weight'], inplace=True)
    df_930955.to_excel('zfile/红利低波及中证全指DUIBI/' + start_date + '_930955.xlsx')

    df_000985 = get_pro().index_weight(index_code='000985.CSI', start_date=start_date, end_date=start_date)
    df_000985.rename(columns={'con_code': 'ts_code'}, inplace=True)
    df_000985['symbol'] = df_000985['ts_code'].str.split('.').str[0]
    df_000985.drop(columns=['weight'], inplace=True)
    df_000985.to_excel('zfile/红利低波及中证全指DUIBI/' + start_date + '_000985.xlsx')

    # 判断df2中的ts_code是否都在df1中
    all_in_df1 = df_930955['ts_code'].isin(df_000985['ts_code']).all()

    print("============================================")
    print("============================================")
    print("============================================")

    print("start_date" + str(start_date))

    print("len(930955)" + str(len(df_930955)))
    print("len(000985)" + str(len(df_000985)))

    if all_in_df1:
        print("df_930955中的所有ts_code都在df_000985中")
    else:
        print("df2中有一些ts_code不在df1中")


def get_data_diff(start_date_930955, start_date_000985):
    engine = get_engine_ts()
    query = f""" select * from stock_basic """
    stock_basic = pd.read_sql_query(query, engine)

    df_930955 = get_pro().index_weight(index_code='930955.CSI', start_date=start_date_930955,
                                       end_date=start_date_930955)
    df_930955.rename(columns={'con_code': 'ts_code'}, inplace=True)
    df_930955['symbol'] = df_930955['ts_code'].str.split('.').str[0]
    df_930955.drop(columns=['weight'], inplace=True)
    df_930955 = pd.merge(df_930955, stock_basic, on='ts_code', how='left')
    df_930955.to_excel('zfile/红利低波及中证全指a/' + start_date_930955 + '_930955.xlsx')

    df_000985 = get_pro().index_weight(index_code='000985.CSI', start_date=start_date_000985,
                                       end_date=start_date_000985)
    df_000985.rename(columns={'con_code': 'ts_code'}, inplace=True)
    df_000985['symbol'] = df_000985['ts_code'].str.split('.').str[0]
    df_000985.drop(columns=['weight'], inplace=True)
    df_000985 = pd.merge(df_000985, stock_basic, on='ts_code', how='left')
    df_000985.to_excel('zfile/红利低波及中证全指a/' + start_date_000985 + '_000985.xlsx')

    # 判断df2中的ts_code是否都在df1中
    all_in_df1 = df_930955['ts_code'].isin(df_000985['ts_code']).all()

    print("============================================")
    print("============================================")
    print("============================================")

    print("start_date_930955" + str(start_date_930955))

    print("len(930955)" + str(len(df_930955)))
    print("len(000985)" + str(len(df_000985)))

    if all_in_df1:
        print("df_930955中的所有ts_code都在df_000985中")
    else:
        print("df2中有一些ts_code不在df1中")


if __name__ == '__main__':
    # get_data_range(start_date='20240615', end_date='20240831')
    # get_data_range(start_date='20240315', end_date='20240531')
    # get_data_range(start_date='20231215', end_date='20240228')
    # get_data_range(start_date='20230915', end_date='20231130')
    #
    # get_data_range(start_date='20230615', end_date='20230831')
    # get_data_range(start_date='20230315', end_date='20230531')
    # get_data_range(start_date='20221215', end_date='20230228')
    # get_data_range(start_date='20220915', end_date='20221130')
    #
    # get_data_range(start_date='20220615', end_date='20220831')
    # get_data_range(start_date='20220315', end_date='20220531')
    # get_data_range(start_date='20211215', end_date='20220228')
    # get_data_range(start_date='20210915', end_date='20211130')

    # # get_data(start_date='20240628')
    # # get_data(start_date='20240430')
    # # get_data(start_date='20240131')
    # # get_data(start_date='20231031')
    # #
    # # get_data(start_date='20230731')
    # # get_data(start_date='20230428')
    # # get_data(start_date='20230131')
    # # get_data(start_date='20221031')
    # #
    # # get_data(start_date='20220729')
    # # get_data(start_date='20220429')
    # # get_data(start_date='20220128')
    # # get_data(start_date='20211029')

    get_data_diff(start_date_930955='20240628', start_date_000985='20240430')
    get_data_diff(start_date_930955='20240430', start_date_000985='20240131')
    get_data_diff(start_date_930955='20240131', start_date_000985='20231031')
    get_data_diff(start_date_930955='20231031', start_date_000985='20230731')
    get_data_diff(start_date_930955='20230731', start_date_000985='20230428')
    get_data_diff(start_date_930955='20230428', start_date_000985='20230131')
    get_data_diff(start_date_930955='20230131', start_date_000985='20221031')
    get_data_diff(start_date_930955='20221031', start_date_000985='20220729')
    get_data_diff(start_date_930955='20220729', start_date_000985='20220429')
    get_data_diff(start_date_930955='20220429', start_date_000985='20220128')
    get_data_diff(start_date_930955='20220128', start_date_000985='20211029')
    # get_data_diff(start_date_930955='20211029',start_date_000985=)
